//
//  ORLoginsViewController.m
//  NewsHots
//
//  Created by rimi on 2016/12/7.
//  Copyright © 2016年 zhongshuilian. All rights reserved.
//

#import "ORLoginsViewController.h"
#import "ORUserTextField.h"
#import "ORMyViewController.h"
#import "ORHomeViewController.h"
#import "ORRegisteredViewController.h"

@interface ORLoginsViewController ()

//背景
@property (nonatomic, strong) UIImageView *backImage;
//用户&&密码
@property (nonatomic, strong) ORUserTextField *userText;
@property (nonatomic, strong) ORUserTextField *passText;
//登录&&注册&&忘记密码
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *registeredButton;
@property (nonatomic, strong) UIButton *forgetButton;
//关闭
@property (nonatomic, strong) UIButton *ShutButton;

@end

@implementation ORLoginsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    
}

- (void)initUI{
    
    
    [self.view addSubview:self.backImage];
    [self.view addSubview:self.userText];
    [self.view addSubview:self.passText];
    [self.view addSubview:self.loginButton];
    [self.view addSubview:self.registeredButton];
    [self.view addSubview:self.forgetButton];
    [self.view addSubview:self.ShutButton];
    self.view.backgroundColor = [UIColor whiteColor];
    
}

- (void)alertWithTitle:(NSString *)title Message:(NSString *)message buttonBlock:(void(^)(void))block {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if (self.block) {
            self.block();
        }
        if ([ORUserSingleton sharedUserInfo].isLogin) {
            [self dismissViewControllerAnimated:YES completion:nil];
            [self showRoundProgressWithTitle:@"加载中"];
            // 多少秒以后执行主线程
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self showRightWithTitle:@"加载成功" autoCloseTime:1];
            });
            

        }else{
            
        }
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
    
}

#pragma mark - Button

- (void)forgetSele{
    
    [self alertWithTitle:@"温馨提示" Message:@"请联系管理员" buttonBlock:nil];
}

- (void)shutSele{
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)registered{
    
    ORRegisteredViewController *registered = [[ORRegisteredViewController alloc]init];
    [self presentViewController:registered animated:NO completion:nil];
    
}

- (void)loginButtonEvent{
    
    NSString *username = self.userText.text;
    NSString *password = self.passText.text;
    if (username && password) {
        // LeanCloud - 登录
        [AVUser logInWithUsernameInBackground:username password:password block:^(AVUser *user, NSError *error) {
            if (error) {
                [self alertWithTitle:@"温馨提示" Message:@"登陆失败" buttonBlock:nil];
            } else {
                
                [ORUserSingleton sharedUserInfo].username = user.username;
                [ORUserSingleton sharedUserInfo].isLogin = YES;
                [ORUserSingleton sharedUserInfo].accountName = [user objectForKey:@"name"];
                AVFile *file = [user objectForKey:@"headImage"];
                [ORUserSingleton sharedUserInfo].headImageUrl = file.url;
                [ORUserSingleton sharedUserInfo].accounsex = [user objectForKey:@"sex"];
                [[ORUserSingleton sharedUserInfo] save];
                
                if (self.block) {
                    self.block();
                }
                [self alertWithTitle:@"温馨提示" Message:@"登陆成功" buttonBlock:^{
                    
                }];
            }
        }];
    }
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#pragma mark - getter

- (UIImageView *)backImage{
    if (!_backImage) {
        _backImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
        _backImage.image = [UIImage imageNamed:@""];
    }
    return _backImage;
}

- (ORUserTextField *)userText{
    if (!_userText) {
        _userText = [[ORUserTextField alloc]initWithFrame:AAdaptionRect(0, 0, 280, 44) WithIconName:@"" WithPlaceholder:@"请输入账号"];
        _userText.center = AAadaptationPoint(kBaseWidth/2, 200/AAdaptationWidth());
        _userText.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _userText;
}

- (ORUserTextField *)passText{
    if (!_passText) {
        _passText = [[ORUserTextField alloc]initWithFrame:AAdaptionRect(0, 0, 280, 44) WithIconName:@"" WithPlaceholder:@"请输入密码"];
        _passText.secureTextEntry = YES;
        _passText.center = AAadaptationPoint(kBaseWidth/2, CGRectGetMaxY(self.userText.frame)/AAdaptationWidth()+30);
        _passText.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _passText;
}

- (UIButton *)loginButton{
    if (!_loginButton) {
        _loginButton = [[UIButton alloc]initWithFrame:AAdaptionRect(0, 0, 260, 44)];
        _loginButton.center = AAadaptationPoint(kBaseWidth/2, CGRectGetMaxY(self.passText.frame)/AAdaptationWidth()+108);
        [_loginButton setTitle:@"登   录" forState:UIControlStateNormal];
        [_loginButton addTarget:self action:@selector(loginButtonEvent) forControlEvents:UIControlEventTouchUpInside];
        _loginButton.layer.cornerRadius = 3;
        _loginButton.backgroundColor = MainColor;
    }
    return _loginButton;
}

- (UIButton *)registeredButton{
    if (!_registeredButton) {
        _registeredButton = [[UIButton alloc]initWithFrame:AAdaptionRect(CGRectGetMaxX(self.passText.frame)/AAdaptationWidth()-50, CGRectGetMaxY(self.passText.frame)/AAdaptationWidth()+5, 55, 22)];
        [_registeredButton setTitle:@"注 册" forState:UIControlStateNormal];
        [_registeredButton addTarget:self action:@selector(registered) forControlEvents:UIControlEventTouchUpInside];
        _registeredButton.titleLabel.font = AAFont(16);
//        _registeredButton.backgroundColor = [UIColor redColor];
        [_registeredButton setTitleColor:MainColor];
    }
    return _registeredButton;
}

- (UIButton *)forgetButton{
    if (!_forgetButton) {
        _forgetButton = [[UIButton alloc]initWithFrame:AAdaptionRect(CGRectGetMinX(self.passText.frame)/AAdaptationWidth(), CGRectGetMaxY(self.passText.frame)/AAdaptationWidth()+5, 100, 22)];
        [_forgetButton setTitle:@"忘记密码？" forState:UIControlStateNormal];
        _forgetButton.titleLabel.font = AAFont(16);
        [_forgetButton setTitleColor:MainColor];
//        _forgetButton.backgroundColor = [UIColor redColor];
        [_forgetButton addTarget:self action:@selector(forgetSele) forControlEvents:UIControlEventTouchUpInside];
    }
    return _forgetButton;
}

- (UIButton *)ShutButton{
    if (!_ShutButton) {
        _ShutButton = [[UIButton alloc]initWithFrame:AAdaptionRect(30, 30, 30, 30)];
        [_ShutButton setImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
        [_ShutButton addTarget:self action:@selector(shutSele) forControlEvents:UIControlEventTouchUpInside];
    }
    return _ShutButton;
}

@end
